import {API} from "@/api/MyAxiosConfig";
import type {JobInterface, ResponseInterface, ResumeAndJobInterface} from "@/model/type";

/**
 * 公共数据接口模块
 */

// 添加职位API
/**
 * @param job 职位信息对象，符合JobInterface类型
 * @returns 返回API响应数据
 */
export async function addJobAPI(job: JobInterface) {
    try {
        const r: ResponseInterface = await API({
            url: "/job/add",
            method: "post",
            data: job,
            headers: {
                "Authorization": "Bearer " + localStorage.getItem("token")
            }
        })
        return r.data
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

// 通过企业token获取职位列表API
/**
 * @param tokenStr 企业认证token
 * @returns 返回API响应数据
 */
export async function getJobListByEnterpriseAPI(tokenStr: string) {
    try {
        const r: ResponseInterface = await API({
            url: "/job/list/enterprise",
            method: "get",
            headers: {
                "Authorization": "Bearer " + tokenStr
            }
        })
        return r.data
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

// 更新职位API
/**
 * @param job 职位信息对象，符合JobInterface类型
 * @returns 返回API响应数据
 */
export async function updateJobAPI(job: JobInterface) {
    try {
        const r: ResponseInterface = await API({
            url: "/job/update",
            method: "post",
            data: job,
            headers: {
                "Authorization": "Bearer " + localStorage.getItem("token")
            }
        })
        return r.data
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

// 删除职位API
/**
 * @param job 职位信息对象，符合JobInterface类型
 * @returns 返回API响应数据
 */
export async function deleteJobAPI(job: JobInterface) {
    try {
        const r: ResponseInterface = await API({
            url: "/job/delete",
            method: "post",
            data: job,
            headers: {
                "Authorization": "Bearer " + localStorage.getItem("token")
            }
        })
        return r.data
    } catch (error) {
        console.error('axios error:', error.message);
    }
}

// 获取职位数量API
/**
 * @returns 返回职位总数
 */
export async function getCountAPI() {
    try {
        const r: ResponseInterface = await API({
            url: "/job/count",
            method: "GET",
            headers: {
                "Authorization": localStorage.getItem("token")
            }
        })
        return r.data
    } catch (e) {
        console.log(e)
    }
}

// 获取职位列表API
/**
 * @param page 请求的页码
 * @param size 每页的职位数量
 * @returns 返回职位列表数据
 */
export async function getJobListAPI(page: number, size: number) {
    try {
        const r: ResponseInterface = await API({
            url: "/job/list/" + page + "/" + size,
            method: "GET",
            headers: {
                "Authorization": localStorage.getItem("token")
            }
        })
        return r.data
    } catch (e) {
        console.log(e)
    }
}

// 提交简历API
/**
 * @param submitObj 包含简历和职位信息的对象，符合ResumeAndJobInterface类型
 * @param tokenStr 用于认证的token字符串
 * @returns 返回API响应数据
 */
export async function submitResumeAPI(submitObj: ResumeAndJobInterface, tokenStr: string) {
    try {
        const r: ResponseInterface = await API({
            url: "/resume_and_job/submit",
            method: "POST",
            data: submitObj,
            headers: {
                "Authorization": tokenStr
            }
        })
        return r.data
    } catch (e) {
        console.log(e)
    }
}